package com.ruoyi.cart.controller;

import com.github.pagehelper.Page;
import com.ruoyi.cart.domain.Shop;
import com.ruoyi.cart.domain.User;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.cart.domain.UserShopsCoupon;
import com.ruoyi.cart.service.IUserShopsCouponService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户优惠券Controller
 *

 * @date 2023-07-23
 */
@RestController
@RequestMapping("/userCoupon")
public class UserShopsCouponController extends BaseController
{
    @Autowired
    private IUserShopsCouponService userShopsCouponService;

    @Autowired
    RemoteUserService remoteUserService;

    /**
     * 查询用户优惠券列表
     */
    @RequiresPermissions("cart:userCoupon:list")
    @GetMapping("/list")
    public TableDataInfo list(UserShopsCoupon userShopsCoupon)
    {
        startPage();
        List<UserShopsCoupon> list = userShopsCouponService.selectUserShopsCouponList(userShopsCoupon);
        return getDataTable(list);
    }
//    /**
//     * 查询用商铺列表
//     */
//    @GetMapping("/listShop")
//    public TableDataInfo listShop(Shop shop)
//    {
//        startPage();
//        List<Shop> list = userShopsCouponService.selectShopList(shop);
//        return getDataTable(list);
//    }

    /**
     * 查询用户列表
     */
    @GetMapping("/listUser")
    public TableDataInfo listUser(SysUser user)
    {
        R<List<SysUser>> userList = remoteUserService.getUserList( SecurityConstants.INNER);
        return getDataTable(userList.getData());
    }

    /**
     * 导出用户优惠券列表
     */
    @RequiresPermissions("cart:userCoupon:export")
    @Log(title = "用户优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserShopsCoupon userShopsCoupon)
    {
        List<UserShopsCoupon> list = userShopsCouponService.selectUserShopsCouponList(userShopsCoupon);
        ExcelUtil<UserShopsCoupon> util = new ExcelUtil<UserShopsCoupon>(UserShopsCoupon.class);
        util.exportExcel(response, list, "用户优惠券数据");
    }

    /**
     * 获取用户优惠券详细信息
     */
    @RequiresPermissions("cart:userCoupon:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userShopsCouponService.selectUserShopsCouponById(id));
    }

    /**
     * 新增用户优惠券
     */
    @RequiresPermissions("cart:userCoupon:add")
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserShopsCoupon userShopsCoupon)
    {
        return toAjax(userShopsCouponService.insertUserShopsCoupon(userShopsCoupon));
    }

    /**
     * 修改用户优惠券
     */
    @RequiresPermissions("cart:userCoupon:edit")
    @Log(title = "用户优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserShopsCoupon userShopsCoupon)
    {
        return toAjax(userShopsCouponService.updateUserShopsCoupon(userShopsCoupon));
    }

    /**
     * 删除用户优惠券
     */
    @RequiresPermissions("cart:userCoupon:remove")
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userShopsCouponService.deleteUserShopsCouponByIds(ids));
    }
}
